package anivis.ui.timeline;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Insets;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Observable;
import java.util.Properties;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.bbn.openmap.I18n;
import com.bbn.openmap.LatLonPoint;
import com.bbn.openmap.LayerHandler;
import com.bbn.openmap.MapBean;
import com.bbn.openmap.MapHandler;
import com.bbn.openmap.PropertyHandler;
import com.bbn.openmap.gui.LayersPanel;
import com.bbn.openmap.gui.OMToolComponent;
import com.bbn.openmap.gui.OMToolSet;
import com.bbn.openmap.gui.WindowSupport;
import com.bbn.openmap.util.Debug;

import anivis.ui.Controller;
import anivis.ui.map.AnimalLayer;
import anivis.ui.timeline.TimelineView;

import java.util.Observer;

public class TimeLineWindow extends OMToolComponent implements Observer {
    
    private JFrame frame = null;
    private AnimalLayer animalLayer = null;
    private Controller controller;
    /**
     * Tool interface method. The retrieval tool's interface. This
     * method creates a button that will bring up the BeanPanel.
     * 
     * @return A container that will contain the 'face' of this panel
     *         on the OpenMap ToolPanel.
     */
    public Container getFace() {
        JButton button = null;

        if (getUseAsTool()) {
            button = new JButton(new ImageIcon("share/img/kalender.gif"));
            button.setBorderPainted(false);
            button.setToolTipText("Open TimeLine");
            button.setMargin(new Insets(0, 0, 0, 0));
            button.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    System.out.println("Opening MyComponent");
                    display();
                }
            });
        }

        return button;
    }
    
    /**
     * Get the ActionListener that triggers the LayersPanel. Useful to have to
     * provide an alternative way to bring up the LayersPanel.
     * 
     * @return ActionListener
     */
    private synchronized void display() {
        if (frame == null) {
            frame = new JFrame("Timeline");
            JPanel panel = new TimelineView(controller);
            
            frame.add(panel);
            
            frame.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent evt) {
                    System.out.println("Closing MyComponent");
                    frame.setVisible(false);
                }
            });
        
            frame.pack();
        }
        frame.setVisible(true);
    }
    
    /**
     * Called when the BeanPanel is added the BeanContext, or when
     * another object is added to the BeanContext after the
     * LayerHandler has been added. This allows the BeanPanel to keep
     * up-to-date with any objects that it may be interested in,
     * namely, the LayerHandler. If a LayerHandler has already been
     * added, the new LayerHandler will replace it.
     * 
     * @param someObj the object being added to the BeanContext.
     */
    public void findAndInit(Object someObj) {
        if (someObj instanceof AnimalLayer) {
            System.out.println("Registering animal layer in MyComponent.");
            this.animalLayer = (AnimalLayer)someObj;
        }
    }

    public void update(Observable o, Object arg) {
        // TODO Auto-generated method stub
        // data should be coming from the controller
    }
}
